﻿//时间 几天前 几分钟前
function getDateTimeStamp(dateStr){
    dateStr=dateStr.replace(/T/g,' ');
    dateStr=dateStr.replace(/\.[0-9]+/g,'');
    return Date.parse(dateStr.replace(/-/gi,"/"));
}
function getDateDiff(dateTimeStamp){
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    if(diffValue < 0){return;}
    var monthC =diffValue/month;
    var weekC =diffValue/(7*day);
    var dayC =diffValue/day;
    var hourC =diffValue/hour;
    var minC =diffValue/minute;
    if(monthC>=1){
        result="" + parseInt(monthC) + "月前";
    }
    else if(weekC>=1){
        result="" + parseInt(weekC) + "周前";
    }
    else if(dayC>=1){
        result=""+ parseInt(dayC) +"天前";
    }
    else if(hourC>=1){
        result=""+ parseInt(hourC) +"小时前";
    }
    else if(minC>=1){
        result=""+ parseInt(minC) +"分钟前";
    }else
        result="刚刚";
    return result;
}
//-----------------------------------------------------------------
$("#logo1").click(function(){
    history.go(-1);
});
if((location.href=="http://www.pulu.com/weixin/searchdetail.html"
    ||location.href=="https://www.pulu.com/weixin/searchdetail.html")
    &&!$("#searchinput").val())  //判断有没有传入参数
{
    $("#pleasewait").css("display","none");
    $(".QueryListTitle").css("display","none");
}
else{
    $("#pleasewait").css("display","block");
    console.log("展示1");
    $(".QueryListTitle").css("display","block");
}
var regs=/(searchKey=)([^\s]+)/;
var searchKey="";
var pageSize=15;
if(urlfrom.match(regs)&&urlfrom.match(regs).splice("=")[2]){
    searchKey=urlfrom.match(regs).splice("=")[2];
}else if(!urlfrom.match(regs)){
    $("#waiting").css("display","none");
    $("#nonews").css("display","block");
}
if(searchKey.match("%")){
    searchKey=decodeURIComponent(searchKey);
}
if($("#searchinput").val()){
    searchKey=decodeURIComponent($("#searchinput").val());
}
$("#page_title1").html(searchKey);
if(searchKey&&searchKey.length>0){
    $("span.query").html(searchKey);
}else{
    $("h2 .query_wrapper").css("display","none");
}

var currentIndex1=1;
//页面切换
$("#choicesearchgoback").click(function(){
    $("#choicesearch").css("display","none");
    $("#page_wrapper1").css("display","block");
});
$("#citygobackbtn").click(function(){
    $("#ctchoicepage").css("display","none");
    $("#page_wrapper").css("display","block");
});
//跳转------------------------------------
function moneytest(e){
    //判断能不能去看详情页
    $.cookie("searchPageCurrentIndex",currentIndex1);
    $.cookie("searchPageTop",$(window).scrollTop());
    if(e.dataset.eid){
        var thiseid= e.dataset.eid;
        location.href='shareexppage.html?eid='+thiseid+"#fromuser="+id;
    }else if(e.dataset.qid){
        var thiseid= e.dataset.qid;
        location.href='questiondetail.html?qid='+thiseid+"#fromuser="+id;
    }else if(e.dataset.newsid){
        var thiseid= e.dataset.newsid;

        var pcUrl=true;
        var errorUrl=/环球网|1905电影网|中国会计视野|编织人生网|IT之家|优姿美女/;
        if(e.dataset.sitename){
            if(errorUrl.test(e.dataset.sitename)){pcUrl=false;}
        }
        if(pcUrl){
            location.href='newsdetail.html?newsid='+thiseid+"#fromuser="+id;
        }
        else{
            location.href='http://www.pulu.com/weixin/newsdetail.html?newsid='+thiseid+"#fromuser="+id;
        }
    }
}
var fromeurl=document.referrer;

var choicesearchurl="https://www.pulu.com/webapi/api/puluquestion/SearchProcQEN_qen?keyword="; //跳转的地址
var frometype=0;//类型
$().ready(function(){
    //来源判断 修改接口
    if(/questionlistj/.test(fromeurl)){
        $("#changeType .option").attr("class","option");
        $("#goExp").attr("class","option active");
        if(!$.cookie("selectType")){
            frometype=3;
            $("title").html("Pulu-经验搜索");
            $("#searchinput").attr("placeholder","输入关键词搜索相关经验");
        }else{
            if(/single|work|house|resource|progect|other/.test($.cookie("selectType"))){
                $("#resourceType").attr("class","option active");
                frometype=410;
                $("#searchinput").attr("placeholder","输入关键词搜索相关资源");
            }
        }
    }else if(/shareexppage/.test(fromeurl)){
        frometype=3;
        $("title").html("Pulu-经验搜索");
        $("#searchinput").attr("placeholder","输入关键词搜索相关经验");
    }else if(/questionlistt/.test(fromeurl)){
        frometype=2;
        $("title").html("Pulu-悬赏搜索");
        $("#searchinput").attr("placeholder","输入关键词搜索相关悬赏");
    }else if(/questiondetail/.test(fromeurl)){
        frometype=2;
        $("title").html("Pulu-悬赏搜索");
        $("#searchinput").attr("placeholder","输入关键词搜索相关悬赏");
    }else if(/subscribe/.test(fromeurl)){
        frometype=1;
        $("title").html("Pulu-资讯搜索");
        $("#searchinput").attr("placeholder","输入关键词搜索相关资讯");
    }else{
        $("#changeType .option").attr("class","option");
        $("#allType").attr("class","option active");
        frometype=0;
        $("#searchinput").attr("placeholder","输入关键词搜索");
    }
    //筛选类型 高级搜索
    if($.cookie("searchType")){
        $("#changeType .option").attr("class","option");
        if($.cookie("searchType")==3){
            $("#goExp").attr("class","option active");
            frometype=3;
            $("title").html("Pulu-经验搜索");
            $("#searchinput").attr("placeholder","输入关键词搜索相关经验");
        }else if($.cookie("searchType")==2){
            $("#goQue").attr("class","option active");
            frometype=2;
            $("title").html("Pulu-悬赏搜索");
            $("#searchinput").attr("placeholder","输入关键词搜索相关悬赏");
        }else if($.cookie("searchType")==1){
            $("#goNews").attr("class","option active");
            frometype=1;
            $("title").html("Pulu-资讯搜索");
            $("#searchinput").attr("placeholder","输入关键词搜索相关资讯");
        }else if($.cookie("searchType")==0){
            $("#allType").attr("class","option active");
            frometype=0;
            $("#searchinput").attr("placeholder","输入关键词搜索");
        }else if($.cookie("searchType")==410){
            $("#resourceType").attr("class","option active");
            frometype=410;
            $("title").html("Pulu-资源搜索");
            $("#searchinput").attr("placeholder","输入关键词搜索相关资源");
        }
    }

    if((location.href)=="http://www.pulu.com/weixin/searchdetail.html"&&!$("#searchinput").val()){

    }
    else{
        if($.cookie("searchPageCurrentIndex")&&/[0-9]/.test($.cookie("searchPageCurrentIndex"))){
            pageSize=parseInt($.cookie("searchPageCurrentIndex"))*15;
        }
        aj(currentIndex1)
    }

});
var canscroll=false;//能否滚动加载 默认不可以
var readyover=0;
function aj(cIndex){
    canscroll=false;
    if(listajax===0&&searchKey){
        $("#pleasewait").css("display","block");
        $(".QueryListTitle").css("display","block");
        $(".query_wrapper").css("display","block");
        $("#waiting").css("display","block");
        $("#nonews").css("display","none");
        //console.log(listajax);
        listajax=1;
        readyover=1;
        $.ajax({
            type:"get",
            datatype:"json",
            url:choicesearchurl+searchKey+"&userId="+id+"&currentIndex="+cIndex+"&pageSize="+pageSize+"&searchtype="+frometype,
            success:function(d){
                if(d.Data&&d.Data.length>0){
                    listajax=0;
                    if(d.Data.length<15){
                        //canscroll=false;
                        $("#waiting").css("display","none");
                        $("#nonews").css("display","block");
                        $("#nonews").html("没有更多搜索结果");
                    }
                    for(var i=0;i<d.Data.length;i++){
                        var regt=/([0-9\-]+)/;
                        var tim=getDateDiff(getDateTimeStamp(d.Data[i].cTime));
                        var hrefto="";
                        if(d.Data[i].userId===id){
                            hrefto="setpage.html";
                        }else{
                            if(d.Data[i].isHideName===1){hrefto=""}else{
                                hrefto="otherpeoplepage.html?otherid="+d.Data[i].userId;
                            }
                        }
                        if(!tim){
                            tim="刚刚";
                        }
                        var realName="";
                        if(d.Data[i].realName){realName=" ("+d.Data[i].realName+")"}
                        var searchnick=d.Data[i].userNick+realName;
                        if(d.Data[i].isHideName===1){searchnick="匿名"};
                        if(d.Data[i].title){
                            //if((d.Data[i].type===1)||(frometype==1)||$.cookie("searchType")==1){
                            if(d.Data[i].type===1){
                                $(".searchlist-box").append("<div class='feed_item' onclick='moneytest(this)' data-sitename='"+d.Data[i].siteName+"' data-newsid='"+d.Data[i].contentId+"'> <div class='rt newslogo'></div><div class='title'> <a class='BoardItemTitle'>"+d.Data[i].title+"</a> </div><div class='EventHeader'> <span><a>"+d.Data[i].siteName+"</a></span> <span class='timestamp'> · "+tim+"</span> </div></div>");
                            }
                            else if(d.Data[i].type===2){
                                var tit=codetoface(d.Data[i].title);
                                //$(".searchlist-box").append("<div class='feed_item' onclick='moneytest(this)' data-qid='"+d.Data[i].contentId+"'><div class='rt quelogo'></div> <div class='title'> <a class='BoardItemTitle'>"+tit+"</a> </div> <div class='EventHeader'> <span><a href='"+hrefto+"'>"+searchnick+"</a></span>  <span class='timestamp'> · "+tim+"</span> <span class='scoreimg'><a><img src='../img/money04.png'/>"+parseFloat(d.Data[i].score)/100+"</a></span> </div> </div>");
                                $(".searchlist-box").append("<div class='feed_item' onclick='moneytest(this)' data-qid='"+d.Data[i].contentId+"'><div class='rt quelogo'></div> <div class='title'> <a class='BoardItemTitle'>"+tit+"</a> </div> <div class='EventHeader'><span class='timestamp'>"+tim+"</span> <span class='scoreimg'><a><img src='../img/money04.png'/>"+parseFloat(d.Data[i].score)/100+"</a></span> </div> </div>");
                            }
                            else if(d.Data[i].type===3){
                                var tit=codetoface(d.Data[i].title);
                                var hiexplogo="";
                                if(d.Data[i].isHigh===1||d.Data[i].isHigh===2){
                                    hiexplogo="<a><img src='img/heiexp.png'/></a>";
                                }
                                //$(".searchlist-box").append("<div class='feed_item' onclick='moneytest(this)' data-eid='"+d.Data[i].contentId+"'>  <div class='rt explogo'></div><div class='title'> <a class='BoardItemTitle'>"+tit+"</a> </div><div class='EventHeader'> <span><a href='"+hrefto+"'>"+searchnick+"</a></span> <span class='timestamp'> · "+tim+"</span> <span class='scoreimg'><a style='position:relative;padding-left:17px;'><img style='position:absolute;top:1px;left: 0;' src='img/money04.png'/>"+parseFloat(d.Data[i].score)/100+"</a>"+hiexplogo+"</span></div></div>  </div>"); // <div class='answer_count_row'> <a  >查看详情</a> </div>
                                $(".searchlist-box").append("<div class='feed_item' onclick='moneytest(this)' data-eid='"+d.Data[i].contentId+"'>  <div class='rt explogo'></div><div class='title'> <a class='BoardItemTitle'>"+tit+"</a> </div><div class='EventHeader'><span class='timestamp'>"+tim+"</span> <span class='scoreimg'><a style='position:relative;padding-left:17px;'><img style='position:absolute;top:1px;left: 0;' src='img/money04.png'/>"+parseFloat(d.Data[i].score)/100+"</a>"+hiexplogo+"</span></div></div>  </div>");
                            }
                            else{
                                var typeLogo=" ziyuan";
                                var tit=codetoface(d.Data[i].title);
                                var scoreCode="";
                                if(d.Data[i].score){
                                    scoreCode="<span class='scoreimg'><a style='position:relative;padding-left:17px;'><img style='position:absolute;top:1px;left: 0;' src='img/money04.png'/>"+parseFloat(d.Data[i].score)/100+"</a></span>"
                                }
                                //$(".searchlist-box").append("<div class='feed_item' onclick='moneytest(this)' data-eid='"+d.Data[i].contentId+"'>  <div class='rt "+typeLogo+"'></div><div class='title'> <a class='BoardItemTitle'>"+tit+"</a> </div><div class='EventHeader'> <span><a href='"+hrefto+"'>"+searchnick+"</a></span> <span class='timestamp'> · "+tim+"</span> "+scoreCode+"</div></div>  </div>");
                                $(".searchlist-box").append("<div class='feed_item' onclick='moneytest(this)' data-eid='"+d.Data[i].contentId+"'>  <div class='rt "+typeLogo+"'></div><div class='title'> <a class='BoardItemTitle'>"+tit+"</a> </div><div class='EventHeader'><span class='timestamp'>"+tim+"</span> "+scoreCode+"</div></div>  </div>");
                            }
                        }
                    }
                }
                else{
                    listajax=0;
                    //canscroll=false;
                    $("#waiting").css("display","none");
                    $("#nonews").css("display","block");
                }
                pageSize=15;
                if($.cookie("searchPageCurrentIndex")&&/[0-9]/.test($.cookie("searchPageCurrentIndex"))){
                    $(window).scrollTop($.cookie("searchPageTop"));
                    currentIndex1=parseInt($.cookie("searchPageCurrentIndex"));
                    $.cookie("searchPageCurrentIndex","");
                    $.cookie("searchPageTop","");
                }
                readyover=0;
                canscroll=true;
            }
        })
    }
}
//var choiced=0;//点击过筛选为1
//筛选按钮 页面切换 高级搜索
$("#gochoicebtn").click(function(){
    //清空上次残留数据
    choiceajax=0;//筛选列表有数据0 清空筛选状态
    hyischoiced=[];//已经选择的行业
    xqischoiced=[];//已经选择的兴趣
    stype="";//筛选类型
    relation="";//关系
    hobby="";//兴趣ID
    job="";//行业ID
    city="";//定位
    education="";//教育

    $(".eduwords").html("");//教育
    $("#relawords").html("");//关系
    $(".myctywords span").remove();//定位
    $(".hangyewords span").remove();//行业
    $(".xingquwords span").remove();//兴趣
    $("#choicedbox span").remove();//行业已经选择
    $("#xqchoicedbox span").remove();//兴趣已经选择

    var edokbtnimg2=document.getElementsByClassName("edokbtnimg2");//学历选中按钮图片2 未选
    var edokbtnimg1=document.getElementsByClassName("edokbtnimg1");//学历选中按钮图片1
    for(var i1=0;i1<edokbtnimg2.length;i1++){
        edokbtnimg2[i1].style.display="block";
        edokbtnimg1[i1].style.display="none";
    }
    $("#page_wrapper1").css("display","none");
    $("#choicesearch").css("display","block");
    $("#waiting").css("display","block");//消息提醒
    $("#nonews").css("display","none");
});
$(window).scroll(function(){
    var mainheight=$(".searchlist-box").height();
    var windowheight=$(window).height();
    var chajuli=$(".searchlist-box").offset().top - $(window).scrollTop();
    var scrollheight=$(window).scrollTop();
    var cz=windowheight-mainheight+5;
    if(chajuli<cz&&canscroll){
        if($("#page_wrapper1").css("display")==="block"&&choiced===0&&canscroll){
            console.log("调用页码："+currentIndex1);
            currentIndex1++;
            aj(currentIndex1);
        }else if($("#page_wrapper1").css("display")==="block"&&choiced===1&&canscroll){
            console.log(currentIndex2);
            currentIndex2++;
            chiocesearchajax(stype,hobby,job,city,education,relation,currentIndex2);
        }
    }
});
//搜索框
document.getElementById("gotosearch").onclick=function(){
    var word=document.getElementById("searchinput").value;
    if(word){
        if(word.match(/^\s+$/g)){
            console.log("没有填信息")
        }else{
            canscroll=true;//刷新滚动
            searchKey=word;
            currentIndex1=1;
            $(".searchlist-box div").remove();
            aj(currentIndex1);
        }
    }
};
document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if(e && e.keyCode==13){ // enter 键
        var word=document.getElementById("searchinput").value;
        if(word&&listajax==0){
            if(word.match(/^\s+$/g)){
                console.log("没有填信息")
            }else{
                $(window).scrollTop(0);
                searchKey=word;
                currentIndex1=1;
                listajax=0;
                canscroll=true;//刷新滚动
                $("h2>div>span.query").html(word);
                $(".searchlist-box div").remove();
                $("#nonews").css("display","none");
                $("#waiting").css("display","block");
                aj(currentIndex1);
            }
        }
    }
};
//切换搜索
$("#changeType").click(function(){
    $(".select").toggle();
});
$("#changeType .option").click(function(i){
    //取消冒泡
    var e=arguments.callee.caller.arguments[0]||event;
    if (e && e.stopPropagation) {
        e.stopPropagation();
    } else if (window.event) {
        window.event.cancelBubble = true;
    }
    //加载完成才可以切换
    if(readyover==0){
        listajax=0;
        var thisid=i.target.id;
        console.log(thisid);
        $("#changeType .option").attr("class","option");
        $("#"+thisid).attr("class","option active");
        $(".select").hide();
        ////数据还原
        $("#waiting1").css("display","block");
        $("#nonews1").css("display","none");
        $("#nonews").css("display","none");
        pageSize=15;
        currentIndex1=1;
        //页面重新加载
        $(window).scrollTop(0);
        $(".searchlist-box div").remove();//清空页面
        switch(thisid){
            case "otherType"://其他
                $.cookie("searchType",10);
                frometype=10;
                aj(currentIndex1);
                break;
            case "resType"://人脉
                $.cookie("searchType",9);
                frometype=9;
                aj(currentIndex1);
                break;
            case "proType"://产品
                $.cookie("searchType",8);
                frometype=8;
                aj(currentIndex1);
                break;
            case "houseType"://住房
                $.cookie("searchType",7);
                frometype=7;
                aj(currentIndex1);
                break;
            case "workType"://工作
                $.cookie("searchType",6);
                frometype=6;
                aj(currentIndex1);
                break;
            case "filmType"://电影
                $.cookie("searchType",5);
                frometype=5;
                aj(currentIndex1);
                break;
            case "singleType"://单身
                $.cookie("searchType",4);
                frometype=4;
                aj(currentIndex1);
                break;
            case "resourceType"://资源
                $.cookie("searchType",410);
                frometype=410;
                aj(currentIndex1);
                break;
            case "expType"://经验
                $.cookie("searchType",3);
                frometype=3;
                aj(currentIndex1);
                break;
            case "queType"://问题
                $.cookie("searchType",2);
                frometype=2;
                aj(currentIndex1);
                break;
            case "newsType"://新闻
                $.cookie("searchType",1);
                frometype=1;
                aj(currentIndex1);
                break;
            case "allType"://全部
                $.cookie("searchType",0);
                frometype=0;
                aj(currentIndex1);
                break;
        }
    }
});